import { defineConfig, loadEnv } from 'vite'
import path from 'path'
import createVitePlugins from './vite/plugins'
import vueJsx from '@vitejs/plugin-vue-jsx' // 添加 vue-jsx 插件导入
import { visualizer } from 'rollup-plugin-visualizer'
import { fileURLToPath } from 'url';
import externalGlobals from 'rollup-plugin-external-globals' // 添加 external-globals 插件导入
import ViteCompression from 'vite-plugin-compression'
import bortli from 'rollup-plugin-brotli'
// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
  const env = loadEnv(mode, process.cwd())
  const { VITE_APP_ENV } = env
  return {
    base: VITE_APP_ENV === 'production' ? '/' : '/',
    plugins: [
      bortli({}),
      // ViteCompression({
      //   verbose: true,
      //   disable: false,
      //   threshold: 1024*20,
      //   algorithm: 'gzip',
      //   ext: '.gz'
      // }),
      ...createVitePlugins(env, command === 'build'),
      vueJsx() // 添加 vueJsx 插件
    ],
    resolve: {
      alias: {
        '~': path.resolve(__dirname, './'),
        '@': path.resolve(__dirname, './src')
      },
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
    },
    server: {
      port: 8888,
      host: true,
      open: true,
      proxy: {
        // '/dev-api': {
        //   target: 'http://hotel.bjdskj.com/api',
        //   changeOrigin: true,
        //   rewrite: (p) => p.replace(/^\/dev-api/, '')
        // }
      }
    },
    // 打包配置
    build: {
      // 关闭 sorcemap 报错不会映射到源码
      sourcemap: false,
      drop_console: true,
      drop_debugger: true,
      // 打包大小超出 400kb 提示警告
      chunkSizeWarningLimit: 400,
      assetsInlineLimit: 4096,
      rollupOptions: {
        plugins:
          [
            visualizer({ open: true }),
            externalGlobals({
              moment: 'moment',
              'video.js': 'videojs',
              jspdf: 'jspdf',
              xlsx: 'xlsx',
            })
          ],


        // 打包入口文件 根目录下的 index.html
        // 也就是项目从哪个文件开始打包
        input: {
          index: fileURLToPath(new URL('./index.html', import.meta.url))
        },

        // 静态资源分类打包
        output: {
          experimentalMinChunkSize: 1000 * 1024,//单位B
          format: 'esm',
          chunkFileNames: 'static/js/[name]-[hash].js',
          entryFileNames: 'static/js/[name]-[hash].js',
          assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
          manualChunks(id) {
            if (typeof id === 'string' && id.includes('node_modules/')) {
              const parts = id.split('node_modules/');
              if (parts.length > 1) {
                return parts[1].split('/')[0];
              }
            }
            return null;
          }
        }
      }
    },
    css: {
      postcss: {
        plugins: [

          {
            postcssPlugin: 'internal:charset-removal',
            AtRule: {
              charset: (atRule) => {
                if (atRule.name === 'charset') {
                  atRule.remove();
                }
              }
            }
          }
        ]
      }
    }
  }
})